package com.wandousoushu.jiusen.ui.search

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.wandousoushu.jiusen.R
import com.wandousoushu.jiusen.base.ItemViewHolder
import com.wandousoushu.jiusen.base.SimpleRecyclerAdapter
import com.wandousoushu.jiusen.booksource.data.entities.SearchBook
import com.wandousoushu.jiusen.help.AppConfig.isNightTheme
import kotlinx.android.synthetic.main.item_search_layout.view.*
import org.jetbrains.anko.backgroundColor
import org.jetbrains.anko.sdk27.listeners.onClick

class SearchAdapter(context: Context, val callBack: CallBack) :
    SimpleRecyclerAdapter<SearchBook>(context, R.layout.item_search_layout) {

    override fun convert(holder: ItemViewHolder, item: SearchBook, payloads: MutableList<Any>) {
        val bundle = payloads.getOrNull(0) as? Bundle
        if (bundle == null) {
            bind(holder.itemView, item)
        } else {
            bindChange(holder.itemView, item, bundle)
        }
    }

    override fun registerListener(holder: ItemViewHolder) {
        holder.itemView.apply {
            onClick {
                getItem(holder.layoutPosition)?.let {
                    callBack.showBookInfo(it.name, it.author)
                }
            }
        }
    }

    private fun bind(itemView: View, searchBook: SearchBook) {
        with(itemView) {
            if (isNightTheme) {
                item_ll_search.setBackgroundColor(Color.BLACK)
            } else {
                item_ll_search.setBackgroundColor(Color.WHITE)
            }
            tv_search_book_name.text = searchBook.name
            tv_search_author.text = context.getString(R.string.search_author_show,searchBook.author)
            //bv_originCount.setBadgeCount(searchBook.origins.size)
            tv_searck_introdution.text = context.getString(R.string.search_intro_show,searchBook.intro)
            tv_search_origin_name.text=context.getString(R.string.search_origin_name_show,searchBook.originName)
            tv_search_origin.text=searchBook.origin
            //iv_cover.load(searchBook.coverUrl, searchBook.name, searchBook.author)

        }
    }

    private fun bindChange(itemView: View, searchBook: SearchBook, bundle: Bundle) {
        with(itemView) {
            if (isNightTheme) {
                item_ll_search.setBackgroundColor(Color.BLACK)
            } else {
                item_ll_search.setBackgroundColor(Color.WHITE)
            }
            bundle.keySet().map {
                when (it) {
                    "name" -> tv_search_book_name.text = searchBook.name
                    "author" -> tv_search_author.text =
                            context.getString(R.string.search_author_show,searchBook.author)
                    "intro" -> tv_searck_introdution.text = context.getString(R.string.search_intro_show,searchBook.intro)
                    "originname" -> tv_searck_introdution.text = context.getString(R.string.search_origin_name_show,searchBook.originName)
                    "origin" -> tv_searck_introdution.text = searchBook.origin
                }
            }
        }
    }

    interface CallBack {
        fun showBookInfo(name: String, author: String)
    }
}